<?php

	class UrlUtil{
		private static $transitDomain = 'https://uri.youyo88.com';// 短链接中转服务器域名
		private static $spreadDomain = 'https://html.youyo88.com';// h5推广页域名
		
		const PADDING = 1;// 62进制数左填充0
		const NO_PADDING = 2;// 62进制数不填充0

		private static $chlidLength = 5;
		private static $appidLength = 3;
        private static $firstMd5Length = 2;
		// 2位62进制可以表示最大3,843的10进制数
		// 3位62进制可以表示最大238,327的10进制数
		// 4位62进制可以表示最大14,776,335的10进制数
		// 5位62进制可以表示最大916,132,831的10进制数
		private static $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

		/**
		 * [initDomain 初始化域名]
		 * @return [type] [description]
		 */
		public static function initDomain() {
			self::$transitDomain = defined('APP_ONLINE')&&APP_ONLINE?self::$transitDomain:'http://192.168.2.161:7708';
		}

		/**
		 * [decToSixTwo 10进制转62进制]
		 * @param  [type] $decimal [description]
		 * @return [type]          [description]
		 */
		public static function decToSixTwo($decimal, $mode = self::NO_PADDING, $length = 11, &$result = '') {
			$decimal = intval($decimal);
			if ($decimal < 0) {
				return false;
			}
			
			if ($decimal >= 62) {
				$mod = $decimal % 62;
				$decimal = floor($decimal / 62);
				$result = self::$chars[$mod] . $result;
				self::decToSixTwo($decimal, $length, $mode, $result);
			}else {
				$result = self::$chars[$decimal] . $result;
			}
			return $mode==self::PADDING?str_pad($result, $length, '0', STR_PAD_LEFT):$result;
		}

		/**
		 * [sixTwoToDec 62进制转10进制]
		 * @param  [type] $sixTwo [description]
		 * @return [type]         [description]
		 */
		public static function sixTwoToDec($sixTwo) {
			if (empty($sixTwo)) {
				return false;
			}
			$sum = 0;
			$length = strlen($sixTwo) - 1;
			$sixTwo = str_split($sixTwo);
			foreach ($sixTwo as $key => $value) {
				$index = array_search($value, self::$chars);
				$sum += $index * pow(62, abs($key - $length));
			}
			return $sum;
		}

		/**
		 * [createShortUrl 生成短链接，所有传入参数都必须为整型，可添加参数]
         * @param  [type] $appid     [description]
         * @param  [type] $channelid [description]
		 * @return [type]            [description]
		 */
		public static function createShortKey($appid,$channelid=10000) {
			$channelid = intval($channelid);
			$appid = intval($appid);
			if ($channelid < 1 || $appid < 1) {
				return false;
			}
			$chlid = self::decToSixTwo($appid, self::PADDING, self::$appidLength);
			$id = self::decToSixTwo($channelid);

            return $chlid . $id;
		}

		/**
		 * [parseShortUrl 分析url参数]
		 * @param  [type] $urlParam [description]
		 * @return [type]           [description]
		 */
		public static function parseShortUrl($urlParam) {
			$appid = substr($urlParam, 0, self::$appidLength);
			$channelid = substr($urlParam, self::$appidLength);
			$args = array();
			$args[0] = $args['channelid'] = self::sixTwoToDec($channelid);
			$args[1] = $args['appid'] = self::sixTwoToDec($appid);
			return $args;
		}


		/**
		 * [protocolPad 填充协议头]
		 * @param  [type] $url  [description]
		 * @param  [type] $type [description]
		 * @return [type]       [description]
		 */
		public static function protocolPad($url) {
			if (strpos($url, '://') === false) {
				if ($_SERVER['SERVER_PROTOCOL']) {
					$protocol = explode('/', $_SERVER['SERVER_PROTOCOL']);
					$protocol = strtolower($protocol[0]) . '://';
				}else {
					$protocol = 'http://';
				}
				$url = $protocol . $url;
			}
			return $url;
		}
        /**
         * [headTo 跳转]
         * @param  [type] $url [description]
         * @param  [type] $appid [description]
         * @return [type]      [description]
         */
        public static function headTo($url) {
            header("HTTP/1.1 302 TEMPORARILY MOVED");
            header("Location: {$url}");
            exit();
        }

        /**
         * [createShortUrlFirst 生成一次性短链接，所有传入参数都必须为整型，可添加参数]
         * @param  [type] $channelid [description]
         * @param  [type] $appid     [description]
         * @param  [type] $type      [1正常包短链，2加速版短链]
         * @return [type]            [description]
         */
        public static function createShortUrlFirst($appid) {
            $appid = intval($appid);
            if ( $appid < 1) {
                return false;
            }
            $random = rand(1000,9999);
            $time = time();
            $daySecond = substr($time,-5);

            $chlid = self::decToSixTwo($appid, self::PADDING, self::$appidLength);
            $daySecondid = self::decToSixTwo($random.$daySecond);
            return $chlid . $daySecondid . substr(md5($daySecondid),0,self::$firstMd5Length);
        }

        public static function parseShortUrlFirst($urlParam) {
            $md5Second = substr($urlParam,-self::$firstMd5Length);
            $urlParam = substr($urlParam,0,strlen($urlParam) - strlen($md5Second));
            $appid = substr($urlParam, 0, self::$appidLength);
            $second = substr($urlParam, self::$appidLength);
            $args = array();
            $args[0] = $args['appid'] = self::sixTwoToDec($appid);
            $args[1] = $args['second'] = self::sixTwoToDec($second);
            if (substr(md5($second),0,self::$firstMd5Length) != $md5Second){
                return array();
            }
            return $args;
        }
	}